package Presentation;

import Metier.Objets.Role;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * Fenêtre principale de l'application;, s'ouvre après s'être connecté
 */
public class FenetrePrincipale extends javax.swing.JFrame {

	private JFrame parent;
        
	/**
	 * Creates new form FenetrePrincipale
	 */
	public FenetrePrincipale() {
		this(null, Role.ADMIN, new Locale("FRENCH"));
	}

	public FenetrePrincipale(JFrame parent, Role role, Locale locale) {
		this(parent, (role == Role.MANAGER || role == Role.ADMIN) ? 0 : (role == Role.VENDEUR) ? 4 : 5, (role == Role.ADMIN) ? 6 : (role == Role.MANAGER) ? 4 : 1, locale);
	}

	public FenetrePrincipale(JFrame parent, int startOnglet, int nbOnglet, Locale locale) {
		this.parent = parent;
		this.setLocale(locale);
		initComponents();
		initTabs(startOnglet, nbOnglet);
	}

	private void initTabs(int startOnglet, int nbOnglet) {
		ResourceBundle mainBundle = ResourceBundle.getBundle("ressources/Main/Main", this.getLocale());
		for (int i = startOnglet; i < startOnglet + nbOnglet && i < 6 && i >= 0; i++) {
			switch (i) {
				case 0:
					tab_pane.addTab(mainBundle.getString("main.onglet.stock"), new OngletStock(this));
					break;
				case 1:
					tab_pane.addTab(mainBundle.getString("main.onglet.vuesynth"), new OngletVueSynth(this));
					break;
				case 2:
					tab_pane.addTab(mainBundle.getString("main.onglet.bilan"), new OngletBilan(this));
					break;
				case 3:
					tab_pane.addTab(mainBundle.getString("main.onglet.config"), new OngletConfig(this));
					break;
				case 4:
					tab_pane.addTab(mainBundle.getString("main.onglet.vendeur"), new OngletVendeur(this));
					break;
				case 5:
					tab_pane.addTab(mainBundle.getString("main.onglet.cuisson"), new OngletCuisson(this));
					break;
			}
		}
	}

	public static void afficherPopupErreur(String message) {
		JOptionPane.showMessageDialog(null, message, "Erreur", JOptionPane.ERROR_MESSAGE);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tab_pane = new javax.swing.JTabbedPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("GPB - Gestion de produits d'une boulangerie");
        setPreferredSize(new java.awt.Dimension(1500, 1000));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tab_pane, javax.swing.GroupLayout.DEFAULT_SIZE, 1066, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tab_pane, javax.swing.GroupLayout.DEFAULT_SIZE, 660, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
		 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(FenetrePrincipale.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		//</editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(() -> {
			new FenetrePrincipale().setVisible(true);
		});
	}

	public void close() {
		if (parent != null) {
			this.setVisible(false);
			this.parent.setVisible(true);
		} else {
			System.exit(0);
		}
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane tab_pane;
    // End of variables declaration//GEN-END:variables
}
